﻿using Yxinmt.Interface;
using Yxinmt.Utility;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Yxinmt.Controls
{
    public class Grid : BaseControl, IControl
    {
        private System.Windows.Controls.Grid _Grid { get; set; }

        public Grid(Dictionary<string, object> property, IComponent component)
        {
            Property = property;
            _Component = component;

            _Grid = new System.Windows.Controls.Grid();
            this.Element = _Grid;

            InitSet();
            this.SetProperty();
            this.SetControlProperty(false, false);
        }

        public override void Add(System.Windows.FrameworkElement control)
        {
            _Grid.Children.Add(control);
        }

        public override void Remove(System.Windows.FrameworkElement control)
        {
            _Grid.Children.Remove(control);
        }

        public override void Clear()
        {
            _Grid.Children.Clear();
            _Grid.RowDefinitions.Clear();
            _Grid.ColumnDefinitions.Clear();
            SetGridColumnsRows();
        }

        private void SetProperty()
        {
        }

        public void SetGridColumnsRows(List<Entity.GridWidthHeight> columnList, List<Entity.GridWidthHeight> rowList)
        {
            if (rowList == null) _Grid.RowDefinitions.Add(new System.Windows.Controls.RowDefinition() { Height = new System.Windows.GridLength(1, System.Windows.GridUnitType.Star) });
            else rowList.ForEach(r =>
            {
                if (r.IsStar) _Grid.RowDefinitions.Add(new System.Windows.Controls.RowDefinition() { Height = new System.Windows.GridLength(r.Height, System.Windows.GridUnitType.Star) });
                else _Grid.RowDefinitions.Add(new System.Windows.Controls.RowDefinition() { Height = new System.Windows.GridLength(r.Height) });
            });

            if (columnList == null) _Grid.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition() { Width = new System.Windows.GridLength(1, System.Windows.GridUnitType.Star) });
            else columnList.ForEach(c =>
            {
                if (c.IsStar) _Grid.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition() { Width = new System.Windows.GridLength(c.Width, System.Windows.GridUnitType.Star) });
                else _Grid.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition() { Width = new System.Windows.GridLength(c.Width) });
            });
        }

        public void SetGridColumnsRows()
        {
            string gridRows = Property.GetStringValue("GridRows");
            string gridColumns = Property.GetStringValue("GridColumns");

            if (!string.IsNullOrEmpty(gridRows))
            {
                gridRows.Split(new char[] { ',', '，' }).ToList().ForEach(r =>
                {
                    if (r == "*") _Grid.RowDefinitions.Add(new System.Windows.Controls.RowDefinition() { Height = new System.Windows.GridLength(1, System.Windows.GridUnitType.Star) });
                    else _Grid.RowDefinitions.Add(new System.Windows.Controls.RowDefinition() { Height = new System.Windows.GridLength(double.Parse(r)) });
                });
            }


            if (!string.IsNullOrEmpty(gridColumns))
            {
                gridColumns.Split(new char[] { ',', '，' }).ToList().ForEach(r =>
                {
                    if (r == "*") _Grid.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition() { Width = new System.Windows.GridLength(1, System.Windows.GridUnitType.Star) });
                    else _Grid.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition() { Width = new System.Windows.GridLength(double.Parse(r)) });
                });
            }
        }

        protected override void SetProperty(string name, object value)
        {
            System.Windows.Controls.Grid c = _Grid;
            switch (name)
            {
                case "name": c.Name = string.Concat("grid_", (string)value); break;
                case "Height": c.Height = Convert.ToDouble(value); break;
                case "Width": c.Width = Convert.ToDouble(value); break;
                case "Dock": System.Windows.Controls.DockPanel.SetDock(c, this.GetDock((string)value)); break;
                case "BackColor": c.Background = GetColor((string)value); break;
                case "ControlAlign": SetControlAlign((string)value); break;
                case "Visible": SetVisible(Convert.ToBoolean(value)); break;
                case "GridRowColumn": SetGridRowColumn((string)value); break;
                case "BackImageUrl": SetBackgroundImage((string)value); break;
                case "ZIndex": SetZIndex(Convert.ToInt32(value)); break;
                case "Margin": c.Margin = this.GetThickness((string)value); SetFactoryProperty("Margin", c.Margin); break;
                case "CanvasLocation": SetCanvasLocation((string)value); break;
                case "CanvasZIndex": SetCanvasZIndex(Convert.ToInt32(value)); break;
            }
        }
    }
}
